package com.hzyl.famousreader.repository.persistence.entity;

import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.StringUtils;
import com.google.gson.annotations.SerializedName;

import java.io.File;

/**
 * 书籍章节
 */
@Entity(tableName = "chapter")
public class BookChapter {

  private static final String CACHE_PATH = PathUtils.getExternalAppCachePath();

  @PrimaryKey
  private Long id;

  @SerializedName("bookId")
  @ColumnInfo(name = "book_id")
  private Long bookId;

  /**
   * 章节标题
   */
  @ColumnInfo(name = "title")
  private String title;

  /**
   * 章节排序（按数量自然增长）
   */
  @ColumnInfo(name = "sort")
  private Integer sort;

  /**
   * 章节内容
   */
  private String path;

  /**
   * 获取文件名
   * @return
   */
  public String getFileName() {
    if(!StringUtils.isTrimEmpty(path)) {
      String[] paths = path.split("/");
      if(paths != null && paths.length > 0){
        return paths[paths.length - 1];
      }
    }
    return null;
  }

  /**
   * 获取文件存储路径
   * @return
   */
  public String getFilePath() {
    String fileName = getFileName();
    if(!StringUtils.isTrimEmpty(fileName)) {
      return CACHE_PATH + "/" + fileName;
    }
    return null;
  }

  /**
   * 文件是否存在
   * @return
   */
  public boolean isFileExist() {
    String filePath = getFilePath();
    if(!StringUtils.isTrimEmpty(filePath)) {
      File file = new File(filePath);
      return file.exists();
    }
    return false;
  }

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public Long getBookId() {
    return bookId;
  }

  public void setBookId(Long bookId) {
    this.bookId = bookId;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public Integer getSort() {
    return sort;
  }

  public void setSort(Integer sort) {
    this.sort = sort;
  }

  public String getPath() {
    return path;
  }

  public void setPath(String path) {
    this.path = path;
  }
}
